<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_tintuc extends CI_Controller {

    private $mIdcat1 = array();
    private $mIdcat2 = array();
    private $mIdcat3 = array();
    private $daotac = array();
    private $datanew = array();
    private $per_page = '';
    private $total_rows = 6;
    private $uri_page = '';
    private $base_page = '';
    private $uri_segment = '';
    private $thongbao = '';
    function __construct() {
        parent::__construct();
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu')=='_en') {
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }
        else{
            $this->rear = '';
            $this->lang->load('atta','vietnam');
        } 
    }

    public function index($id, $alias) {
        //$alias =$this->security->xss_clean($this->uri->segment(1,0));//cat duoi html
        //Selec danhmuctt by alias
        $getCategoryByAlias = $this->common_model->getone('danhmuctt', array('publish' => 1, 'id' => $id));
        //Get service by iddanhmuc
        if (!empty($getCategoryByAlias)) {
            $aliasCategories = $alias;
            switch ($aliasCategories) {
                case 'gioi-thieu':
                    $bv_gd = $this->common_model->getone('setting',array('id'=>1))->baigioithieu;
                    $gioithieu['content_gt'] = $this->common_model->getone('tintuc',array('id'=>$bv_gd));
                    $gioithieu['metades'] = $gioithieu['content_gt']->metades;
                    $gioithieu['metakey'] = $gioithieu['content_gt']->metakey;
                    $gioithieu['rear'] = $this->rear;
                    $gioithieu['title'] = 'Giới thiệu';
                    $data['content'] = $this->load->view('mod_tintuc/views_gioithieu.php',$gioithieu, true);
                    break;
                default:
                    $menu = $this->common_model->getdata('danhmuctt', array('publish' => 1));
                    $getCateAlias = $this->common_model->getdata('alias', array('type' => 'danhmuctt'));
                    foreach ($getCateAlias as $ali) {
                        $dataAlias[$ali->a_id] = $ali;
                    }
                    $getAlias = $this->common_model->getdata('alias', array('type' => 'tintuc'));
                    foreach ($getAlias as $ali) {
                        $dataAliastt[$ali->a_id] = $ali;
                    }
                    foreach ($menu as $mn) {
                        $pcat[$mn->parentid][] = $mn;
                        $pcat1[$mn->id] = $mn;
                    }
                    $mang = $this->common_model->getcat($pcat, $getCategoryByAlias->id, array());
                    $mcat = $this->common_model->category_tt1($getCategoryByAlias->id, $pcat1, array());
                    if (!empty($mang)) {
                        $offset = $this->uri->segment(2);
                        $this->per_page = 24;
                        $this->base_page = $this->uri->segment(1) . '/';
                        $this->uri_segment = 2;
                        $mNews = $this->common_model->getlist('tintuc', array('iddanhmuc', $mang), array('publish' => 1), '', array('id', 'DESC'), array($this->per_page, $offset));
                        $this->total_rows = $this->common_model->getnumrow('tintuc', array('iddanhmuc', $mang), array('publish' => 1));
                        $this->phantrang();
                        if (!empty($mNews)) {
                            // var_dump($mNews);
                            $data['content'] = $this->load->view('mod_tintuc/views_listtintuc', array('categories' => $mcat, 'mNews' => $mNews, 'dataAliastt' => $dataAliastt,'dataAlias'=>$dataAlias,'rear'=>$this->rear), true);
                        } else {
                           $data['content'] = $this->load->view('mod_tintuc/views_listtintuc', array('categories' => $mcat,'dataAliastt' => $dataAliastt,'dataAlias'=>$dataAlias,'getCategoryByAlias'=>$getCategoryByAlias,'rear'=>$this->rear), true);
                        }
                    } else {
                        $data['content'] = $this->load->view('view_404', '', true);
                    }
                    if ($getCategoryByAlias->title_seo != '') {
                        $data['title'] = $getCategoryByAlias->title_seo;
                    } else {
                        $data['title'] = $getCategoryByAlias->title;
                    }
                    $data['cat_title'] = $getCategoryByAlias->title;
                    $data['metades'] = $getCategoryByAlias->metades;
                    $data['metakey'] = $getCategoryByAlias->metakey;
                    break;
            }
        }
        $this->load->view('index', $data);
    }

    public function detail($id, $aliasCategories) {
        $dat['rear'] = $this->rear;
        $dat['news'] = $this->common_model->getone('tintuc',array('id'=>$id));
        $data['content'] = $this->load->view('mod_tintuc/views_detailtt.php',$dat,true);
        $this->load->view('index',$data);
    }

    function phantrang() {
        $this->load->library('pagination');
        $config['base_url'] = base_url() . $this->base_page;
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = $this->uri_segment;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
    }

}
